import os, time
from pprint import pprint

# refer
# https://www.chuanying520.com/trial/5593.html
# https://www.chuangshi36.com/trial/8610.html

# ffmpeg -f lavfi -i color=LightCyan:640x480:d=3 -format rgb32 -f matroska  test1.mp4
# ffplay  -f lavfi -i color=Black:640x480:d=3

# PAR —— Pixel Aspect Ratio 像素横纵比。表示每个像素的宽度与长度的比值。可以认为每个像素不是正方形的。
# DAR —— Display Aspect Ratio 显示横纵比。最终显示的图像在长度单位上的横纵比。
# SAR —— Sample Aspect Ratio 采样横纵比。表示横向的像素点数和纵向的像素点数的比值。
# 链接：https://www.jianshu.com/p/91ba0c9dc165

numlists=[
    'zero',
    'one',
    'two',
    'three',
    'four',
    'five',
    'six',
    'seven',
    'eight',
    'nine',
    'ten',
]

glslfile = '/home/mc/taozhuo/ffmpeg-gl-transition/gl-transitions/transitions/heart.glsl'
glslfile = './heart.glsl'
fontfile = '../open-sans/OpenSans-Regular.ttf:text'
sname = 'qianshou'
fontsize = 48

# AliceBlue’‘AntiqueWhite’‘Aqua’‘Aquamarine’‘Azure’‘Beige’‘Bisque’
# https://ffmpeg.org/ffmpeg-utils.html   2.7 Color

# drawFilter= f'[1:v]drawbox=y=ih/PHI:color=black@0.4:width=iw/2:height=48:t=fill,drawtext=fontfile=../open-sans/OpenSans-Regular.ttf:text=\'hello\':fontcolor=white:fontsize=24:x=(w-tw)/2:y=(h/PHI)+th,format=yuv420p [tmp]'

index = 0
input = ''
drawFilter = ''
iconcat = ''
for num in numlists:
    if index ==0:
        input = f' -t 1.5   -f lavfi -i color=Black:820x546:d=3  -t 1.5 -loop 1  -i {sname}{index}.jpg '
    else:
        input = f' -t 1.5 -loop 1  -i {sname}{index-1}.jpg  -t 1.5 -loop 1  -i {sname}{index}.jpg '
    scalePic =  f'[0:v]scale=820x546,setsar=1/1 [stmp0];[1:v]scale=820x546,setsar=1/1 [stmp1]'
    drawFilter= f'{scalePic};[stmp1]drawtext=fontfile={fontfile}=\'{numlists[len(numlists) - index - 1].upper()}\':\
fontcolor=white:fontsize={fontsize}:x=(w-tw)/2:y=(h/2)-th,format=yuv420p [tmp]'

    gltransition =  f'{drawFilter};[stmp0][tmp]gltransition=offset=0.0:duration=1.0:source={glslfile}[v1];[v1]scale=820x546 [v2]'
    ffmpegcmd = f'ffmpeg  {input}  -filter_complex  "{gltransition}"   -map "[v2]" -y output/{index}.mp4'
    print('ffmpegcmd is ', ffmpegcmd)
    os.system(ffmpegcmd)   
    index = index +1

iconcat = ''
input = ''
for i in range (len(numlists)):
    iconcat = f'{iconcat}[{i}:v]'
    input = f'{input} -i output/{i}.mp4 '

iconcat = f'ffmpeg {input} -filter_complex "{iconcat}concat=n={len(numlists)} [v]" -map "[v]" -y  output/out.mp4'
print('iconcat is ', iconcat )
os.system(iconcat)   

# https://stackoverflow.com/questions/7333232/how-to-concatenate-two-mp4-files-using-ffmpeg
# ffmpeg -i opening.mkv -i episode.mkv -i ending.mkv \
#        -filter_complex "[0:v] [0:a] [1:v] [1:a] [2:v] [2:a] 
#                         concat=n=3:v=1:a=1 [v] [a]" \
#        -map "[v]" -map "[a]" output.mkv


#  vconcat = f'{vconcat} [tmp{index}]'  # [v1] [v2] concat [outv]  http://ffmpeg.org/ffmpeg-filters.html#Examples-136
# print('input is ', input)
# print('drawFilter is ', drawFilter)
# print('vconcat is ', vconcat)


# os.system('rm  w.mp4')
# drawFilter= f'[1:v]drawtext=fontfile={fontfile}=\'{numlists[index].capitalize()}\':\
# fontcolor=white:fontsize={fontsize}:x=(w-tw)/2:y=(h/2)-th,format=yuv420p [tmp]'
# ffmpegcmd = f'ffmpeg \
# -t 1.5 -loop 1  -i   {sname}1.jpg  \
# -t 1.5 -loop 1  -i   {sname}2.jpg   \
# -filter_complex  \
# "{drawFilter};[0:v][tmp]gltransition=offset=0.1:duration=0.5:source={glslfile}" \
# -y w.mp4'
# print(f'ffmpegcmd is {ffmpegcmd}')
# os.system(ffmpegcmd)
# os.system('ffplay w.mp4')



# showText = f'{glslTitles[index]} Transition Effect'.capitalize()
# showText = f'{glslTitles[index]} Transition Effect ( {index}/{total} )'.title()
# showText = f'{numlists[0]} '.title()
# os.system(ffmpegcmd)
# print(f'{ffmpegcmd}')
# ffmpegTextCmd = f"ffmpeg -y -i output/{index}.mp4 -vf \"format=yuv444p,drawbox=y=ih/PHI:color=black@0.4:width=iw:height=48:t=fill,drawtext=fontfile=open-sans/OpenSans-Regular.ttf:text='{showText}':fontcolor=white:fontsize=24:x=(w-tw)/2:y=(h/PHI)+th,format=yuv420p'\" -c:v libx264 -c:a copy -movflags +faststart outputtext/{index}.mp4"
# os.system(ffmpegTextCmd)


